{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1 方便处理多维度数组或者矩阵之间的运算\n",
    "2 在处理多维度数组或者矩阵，运算效率高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 12, 36, 80, 150, 252, 392, 576, 810]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def python_test(n):\n",
    "    a = [i**2 for i in range(n)]\n",
    "    b = [i**3 for i in range(n)]\n",
    "    c = []\n",
    "    for i in range(n):\n",
    "        c.append(a[i]+b[i])\n",
    "    return c\n",
    "python_test(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0,   2,  12,  36,  80, 150, 252, 392, 576, 810], dtype=int32)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "def numpy_test(n):\n",
    "    a = np.arange(n)**2\n",
    "    b = np.arange(n)**3\n",
    "    c = a+b\n",
    "    return c\n",
    "numpy_test(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 11.2 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 2,\n",
       " 12,\n",
       " 36,\n",
       " 80,\n",
       " 150,\n",
       " 252,\n",
       " 392,\n",
       " 576,\n",
       " 810,\n",
       " 1100,\n",
       " 1452,\n",
       " 1872,\n",
       " 2366,\n",
       " 2940,\n",
       " 3600,\n",
       " 4352,\n",
       " 5202,\n",
       " 6156,\n",
       " 7220,\n",
       " 8400,\n",
       " 9702,\n",
       " 11132,\n",
       " 12696,\n",
       " 14400,\n",
       " 16250,\n",
       " 18252,\n",
       " 20412,\n",
       " 22736,\n",
       " 25230,\n",
       " 27900,\n",
       " 30752,\n",
       " 33792,\n",
       " 37026,\n",
       " 40460,\n",
       " 44100,\n",
       " 47952,\n",
       " 52022,\n",
       " 56316,\n",
       " 60840,\n",
       " 65600,\n",
       " 70602,\n",
       " 75852,\n",
       " 81356,\n",
       " 87120,\n",
       " 93150,\n",
       " 99452,\n",
       " 106032,\n",
       " 112896,\n",
       " 120050,\n",
       " 127500,\n",
       " 135252,\n",
       " 143312,\n",
       " 151686,\n",
       " 160380,\n",
       " 169400,\n",
       " 178752,\n",
       " 188442,\n",
       " 198476,\n",
       " 208860,\n",
       " 219600,\n",
       " 230702,\n",
       " 242172,\n",
       " 254016,\n",
       " 266240,\n",
       " 278850,\n",
       " 291852,\n",
       " 305252,\n",
       " 319056,\n",
       " 333270,\n",
       " 347900,\n",
       " 362952,\n",
       " 378432,\n",
       " 394346,\n",
       " 410700,\n",
       " 427500,\n",
       " 444752,\n",
       " 462462,\n",
       " 480636,\n",
       " 499280,\n",
       " 518400,\n",
       " 538002,\n",
       " 558092,\n",
       " 578676,\n",
       " 599760,\n",
       " 621350,\n",
       " 643452,\n",
       " 666072,\n",
       " 689216,\n",
       " 712890,\n",
       " 737100,\n",
       " 761852,\n",
       " 787152,\n",
       " 813006,\n",
       " 839420,\n",
       " 866400,\n",
       " 893952,\n",
       " 922082,\n",
       " 950796,\n",
       " 980100,\n",
       " 1010000,\n",
       " 1040502,\n",
       " 1071612,\n",
       " 1103336,\n",
       " 1135680,\n",
       " 1168650,\n",
       " 1202252,\n",
       " 1236492,\n",
       " 1271376,\n",
       " 1306910,\n",
       " 1343100,\n",
       " 1379952,\n",
       " 1417472,\n",
       " 1455666,\n",
       " 1494540,\n",
       " 1534100,\n",
       " 1574352,\n",
       " 1615302,\n",
       " 1656956,\n",
       " 1699320,\n",
       " 1742400,\n",
       " 1786202,\n",
       " 1830732,\n",
       " 1875996,\n",
       " 1922000,\n",
       " 1968750,\n",
       " 2016252,\n",
       " 2064512,\n",
       " 2113536,\n",
       " 2163330,\n",
       " 2213900,\n",
       " 2265252,\n",
       " 2317392,\n",
       " 2370326,\n",
       " 2424060,\n",
       " 2478600,\n",
       " 2533952,\n",
       " 2590122,\n",
       " 2647116,\n",
       " 2704940,\n",
       " 2763600,\n",
       " 2823102,\n",
       " 2883452,\n",
       " 2944656,\n",
       " 3006720,\n",
       " 3069650,\n",
       " 3133452,\n",
       " 3198132,\n",
       " 3263696,\n",
       " 3330150,\n",
       " 3397500,\n",
       " 3465752,\n",
       " 3534912,\n",
       " 3604986,\n",
       " 3675980,\n",
       " 3747900,\n",
       " 3820752,\n",
       " 3894542,\n",
       " 3969276,\n",
       " 4044960,\n",
       " 4121600,\n",
       " 4199202,\n",
       " 4277772,\n",
       " 4357316,\n",
       " 4437840,\n",
       " 4519350,\n",
       " 4601852,\n",
       " 4685352,\n",
       " 4769856,\n",
       " 4855370,\n",
       " 4941900,\n",
       " 5029452,\n",
       " 5118032,\n",
       " 5207646,\n",
       " 5298300,\n",
       " 5390000,\n",
       " 5482752,\n",
       " 5576562,\n",
       " 5671436,\n",
       " 5767380,\n",
       " 5864400,\n",
       " 5962502,\n",
       " 6061692,\n",
       " 6161976,\n",
       " 6263360,\n",
       " 6365850,\n",
       " 6469452,\n",
       " 6574172,\n",
       " 6680016,\n",
       " 6786990,\n",
       " 6895100,\n",
       " 7004352,\n",
       " 7114752,\n",
       " 7226306,\n",
       " 7339020,\n",
       " 7452900,\n",
       " 7567952,\n",
       " 7684182,\n",
       " 7801596,\n",
       " 7920200,\n",
       " 8040000,\n",
       " 8161002,\n",
       " 8283212,\n",
       " 8406636,\n",
       " 8531280,\n",
       " 8657150,\n",
       " 8784252,\n",
       " 8912592,\n",
       " 9042176,\n",
       " 9173010,\n",
       " 9305100,\n",
       " 9438452,\n",
       " 9573072,\n",
       " 9708966,\n",
       " 9846140,\n",
       " 9984600,\n",
       " 10124352,\n",
       " 10265402,\n",
       " 10407756,\n",
       " 10551420,\n",
       " 10696400,\n",
       " 10842702,\n",
       " 10990332,\n",
       " 11139296,\n",
       " 11289600,\n",
       " 11441250,\n",
       " 11594252,\n",
       " 11748612,\n",
       " 11904336,\n",
       " 12061430,\n",
       " 12219900,\n",
       " 12379752,\n",
       " 12540992,\n",
       " 12703626,\n",
       " 12867660,\n",
       " 13033100,\n",
       " 13199952,\n",
       " 13368222,\n",
       " 13537916,\n",
       " 13709040,\n",
       " 13881600,\n",
       " 14055602,\n",
       " 14231052,\n",
       " 14407956,\n",
       " 14586320,\n",
       " 14766150,\n",
       " 14947452,\n",
       " 15130232,\n",
       " 15314496,\n",
       " 15500250,\n",
       " 15687500,\n",
       " 15876252,\n",
       " 16066512,\n",
       " 16258286,\n",
       " 16451580,\n",
       " 16646400,\n",
       " 16842752,\n",
       " 17040642,\n",
       " 17240076,\n",
       " 17441060,\n",
       " 17643600,\n",
       " 17847702,\n",
       " 18053372,\n",
       " 18260616,\n",
       " 18469440,\n",
       " 18679850,\n",
       " 18891852,\n",
       " 19105452,\n",
       " 19320656,\n",
       " 19537470,\n",
       " 19755900,\n",
       " 19975952,\n",
       " 20197632,\n",
       " 20420946,\n",
       " 20645900,\n",
       " 20872500,\n",
       " 21100752,\n",
       " 21330662,\n",
       " 21562236,\n",
       " 21795480,\n",
       " 22030400,\n",
       " 22267002,\n",
       " 22505292,\n",
       " 22745276,\n",
       " 22986960,\n",
       " 23230350,\n",
       " 23475452,\n",
       " 23722272,\n",
       " 23970816,\n",
       " 24221090,\n",
       " 24473100,\n",
       " 24726852,\n",
       " 24982352,\n",
       " 25239606,\n",
       " 25498620,\n",
       " 25759400,\n",
       " 26021952,\n",
       " 26286282,\n",
       " 26552396,\n",
       " 26820300,\n",
       " 27090000,\n",
       " 27361502,\n",
       " 27634812,\n",
       " 27909936,\n",
       " 28186880,\n",
       " 28465650,\n",
       " 28746252,\n",
       " 29028692,\n",
       " 29312976,\n",
       " 29599110,\n",
       " 29887100,\n",
       " 30176952,\n",
       " 30468672,\n",
       " 30762266,\n",
       " 31057740,\n",
       " 31355100,\n",
       " 31654352,\n",
       " 31955502,\n",
       " 32258556,\n",
       " 32563520,\n",
       " 32870400,\n",
       " 33179202,\n",
       " 33489932,\n",
       " 33802596,\n",
       " 34117200,\n",
       " 34433750,\n",
       " 34752252,\n",
       " 35072712,\n",
       " 35395136,\n",
       " 35719530,\n",
       " 36045900,\n",
       " 36374252,\n",
       " 36704592,\n",
       " 37036926,\n",
       " 37371260,\n",
       " 37707600,\n",
       " 38045952,\n",
       " 38386322,\n",
       " 38728716,\n",
       " 39073140,\n",
       " 39419600,\n",
       " 39768102,\n",
       " 40118652,\n",
       " 40471256,\n",
       " 40825920,\n",
       " 41182650,\n",
       " 41541452,\n",
       " 41902332,\n",
       " 42265296,\n",
       " 42630350,\n",
       " 42997500,\n",
       " 43366752,\n",
       " 43738112,\n",
       " 44111586,\n",
       " 44487180,\n",
       " 44864900,\n",
       " 45244752,\n",
       " 45626742,\n",
       " 46010876,\n",
       " 46397160,\n",
       " 46785600,\n",
       " 47176202,\n",
       " 47568972,\n",
       " 47963916,\n",
       " 48361040,\n",
       " 48760350,\n",
       " 49161852,\n",
       " 49565552,\n",
       " 49971456,\n",
       " 50379570,\n",
       " 50789900,\n",
       " 51202452,\n",
       " 51617232,\n",
       " 52034246,\n",
       " 52453500,\n",
       " 52875000,\n",
       " 53298752,\n",
       " 53724762,\n",
       " 54153036,\n",
       " 54583580,\n",
       " 55016400,\n",
       " 55451502,\n",
       " 55888892,\n",
       " 56328576,\n",
       " 56770560,\n",
       " 57214850,\n",
       " 57661452,\n",
       " 58110372,\n",
       " 58561616,\n",
       " 59015190,\n",
       " 59471100,\n",
       " 59929352,\n",
       " 60389952,\n",
       " 60852906,\n",
       " 61318220,\n",
       " 61785900,\n",
       " 62255952,\n",
       " 62728382,\n",
       " 63203196,\n",
       " 63680400,\n",
       " 64160000,\n",
       " 64642002,\n",
       " 65126412,\n",
       " 65613236,\n",
       " 66102480,\n",
       " 66594150,\n",
       " 67088252,\n",
       " 67584792,\n",
       " 68083776,\n",
       " 68585210,\n",
       " 69089100,\n",
       " 69595452,\n",
       " 70104272,\n",
       " 70615566,\n",
       " 71129340,\n",
       " 71645600,\n",
       " 72164352,\n",
       " 72685602,\n",
       " 73209356,\n",
       " 73735620,\n",
       " 74264400,\n",
       " 74795702,\n",
       " 75329532,\n",
       " 75865896,\n",
       " 76404800,\n",
       " 76946250,\n",
       " 77490252,\n",
       " 78036812,\n",
       " 78585936,\n",
       " 79137630,\n",
       " 79691900,\n",
       " 80248752,\n",
       " 80808192,\n",
       " 81370226,\n",
       " 81934860,\n",
       " 82502100,\n",
       " 83071952,\n",
       " 83644422,\n",
       " 84219516,\n",
       " 84797240,\n",
       " 85377600,\n",
       " 85960602,\n",
       " 86546252,\n",
       " 87134556,\n",
       " 87725520,\n",
       " 88319150,\n",
       " 88915452,\n",
       " 89514432,\n",
       " 90116096,\n",
       " 90720450,\n",
       " 91327500,\n",
       " 91937252,\n",
       " 92549712,\n",
       " 93164886,\n",
       " 93782780,\n",
       " 94403400,\n",
       " 95026752,\n",
       " 95652842,\n",
       " 96281676,\n",
       " 96913260,\n",
       " 97547600,\n",
       " 98184702,\n",
       " 98824572,\n",
       " 99467216,\n",
       " 100112640,\n",
       " 100760850,\n",
       " 101411852,\n",
       " 102065652,\n",
       " 102722256,\n",
       " 103381670,\n",
       " 104043900,\n",
       " 104708952,\n",
       " 105376832,\n",
       " 106047546,\n",
       " 106721100,\n",
       " 107397500,\n",
       " 108076752,\n",
       " 108758862,\n",
       " 109443836,\n",
       " 110131680,\n",
       " 110822400,\n",
       " 111516002,\n",
       " 112212492,\n",
       " 112911876,\n",
       " 113614160,\n",
       " 114319350,\n",
       " 115027452,\n",
       " 115738472,\n",
       " 116452416,\n",
       " 117169290,\n",
       " 117889100,\n",
       " 118611852,\n",
       " 119337552,\n",
       " 120066206,\n",
       " 120797820,\n",
       " 121532400,\n",
       " 122269952,\n",
       " 123010482,\n",
       " 123753996,\n",
       " 124500500,\n",
       " 125250000,\n",
       " 126002502,\n",
       " 126758012,\n",
       " 127516536,\n",
       " 128278080,\n",
       " 129042650,\n",
       " 129810252,\n",
       " 130580892,\n",
       " 131354576,\n",
       " 132131310,\n",
       " 132911100,\n",
       " 133693952,\n",
       " 134479872,\n",
       " 135268866,\n",
       " 136060940,\n",
       " 136856100,\n",
       " 137654352,\n",
       " 138455702,\n",
       " 139260156,\n",
       " 140067720,\n",
       " 140878400,\n",
       " 141692202,\n",
       " 142509132,\n",
       " 143329196,\n",
       " 144152400,\n",
       " 144978750,\n",
       " 145808252,\n",
       " 146640912,\n",
       " 147476736,\n",
       " 148315730,\n",
       " 149157900,\n",
       " 150003252,\n",
       " 150851792,\n",
       " 151703526,\n",
       " 152558460,\n",
       " 153416600,\n",
       " 154277952,\n",
       " 155142522,\n",
       " 156010316,\n",
       " 156881340,\n",
       " 157755600,\n",
       " 158633102,\n",
       " 159513852,\n",
       " 160397856,\n",
       " 161285120,\n",
       " 162175650,\n",
       " 163069452,\n",
       " 163966532,\n",
       " 164866896,\n",
       " 165770550,\n",
       " 166677500,\n",
       " 167587752,\n",
       " 168501312,\n",
       " 169418186,\n",
       " 170338380,\n",
       " 171261900,\n",
       " 172188752,\n",
       " 173118942,\n",
       " 174052476,\n",
       " 174989360,\n",
       " 175929600,\n",
       " 176873202,\n",
       " 177820172,\n",
       " 178770516,\n",
       " 179724240,\n",
       " 180681350,\n",
       " 181641852,\n",
       " 182605752,\n",
       " 183573056,\n",
       " 184543770,\n",
       " 185517900,\n",
       " 186495452,\n",
       " 187476432,\n",
       " 188460846,\n",
       " 189448700,\n",
       " 190440000,\n",
       " 191434752,\n",
       " 192432962,\n",
       " 193434636,\n",
       " 194439780,\n",
       " 195448400,\n",
       " 196460502,\n",
       " 197476092,\n",
       " 198495176,\n",
       " 199517760,\n",
       " 200543850,\n",
       " 201573452,\n",
       " 202606572,\n",
       " 203643216,\n",
       " 204683390,\n",
       " 205727100,\n",
       " 206774352,\n",
       " 207825152,\n",
       " 208879506,\n",
       " 209937420,\n",
       " 210998900,\n",
       " 212063952,\n",
       " 213132582,\n",
       " 214204796,\n",
       " 215280600,\n",
       " 216360000,\n",
       " 217443002,\n",
       " 218529612,\n",
       " 219619836,\n",
       " 220713680,\n",
       " 221811150,\n",
       " 222912252,\n",
       " 224016992,\n",
       " 225125376,\n",
       " 226237410,\n",
       " 227353100,\n",
       " 228472452,\n",
       " 229595472,\n",
       " 230722166,\n",
       " 231852540,\n",
       " 232986600,\n",
       " 234124352,\n",
       " 235265802,\n",
       " 236410956,\n",
       " 237559820,\n",
       " 238712400,\n",
       " 239868702,\n",
       " 241028732,\n",
       " 242192496,\n",
       " 243360000,\n",
       " 244531250,\n",
       " 245706252,\n",
       " 246885012,\n",
       " 248067536,\n",
       " 249253830,\n",
       " 250443900,\n",
       " 251637752,\n",
       " 252835392,\n",
       " 254036826,\n",
       " 255242060,\n",
       " 256451100,\n",
       " 257663952,\n",
       " 258880622,\n",
       " 260101116,\n",
       " 261325440,\n",
       " 262553600,\n",
       " 263785602,\n",
       " 265021452,\n",
       " 266261156,\n",
       " 267504720,\n",
       " 268752150,\n",
       " 270003452,\n",
       " 271258632,\n",
       " 272517696,\n",
       " 273780650,\n",
       " 275047500,\n",
       " 276318252,\n",
       " 277592912,\n",
       " 278871486,\n",
       " 280153980,\n",
       " 281440400,\n",
       " 282730752,\n",
       " 284025042,\n",
       " 285323276,\n",
       " 286625460,\n",
       " 287931600,\n",
       " 289241702,\n",
       " 290555772,\n",
       " 291873816,\n",
       " 293195840,\n",
       " 294521850,\n",
       " 295851852,\n",
       " 297185852,\n",
       " 298523856,\n",
       " 299865870,\n",
       " 301211900,\n",
       " 302561952,\n",
       " 303916032,\n",
       " 305274146,\n",
       " 306636300,\n",
       " 308002500,\n",
       " 309372752,\n",
       " 310747062,\n",
       " 312125436,\n",
       " 313507880,\n",
       " 314894400,\n",
       " 316285002,\n",
       " 317679692,\n",
       " 319078476,\n",
       " 320481360,\n",
       " 321888350,\n",
       " 323299452,\n",
       " 324714672,\n",
       " 326134016,\n",
       " 327557490,\n",
       " 328985100,\n",
       " 330416852,\n",
       " 331852752,\n",
       " 333292806,\n",
       " 334737020,\n",
       " 336185400,\n",
       " 337637952,\n",
       " 339094682,\n",
       " 340555596,\n",
       " 342020700,\n",
       " 343490000,\n",
       " 344963502,\n",
       " 346441212,\n",
       " 347923136,\n",
       " 349409280,\n",
       " 350899650,\n",
       " 352394252,\n",
       " 353893092,\n",
       " 355396176,\n",
       " 356903510,\n",
       " 358415100,\n",
       " 359930952,\n",
       " 361451072,\n",
       " 362975466,\n",
       " 364504140,\n",
       " 366037100,\n",
       " 367574352,\n",
       " 369115902,\n",
       " 370661756,\n",
       " 372211920,\n",
       " 373766400,\n",
       " 375325202,\n",
       " 376888332,\n",
       " 378455796,\n",
       " 380027600,\n",
       " 381603750,\n",
       " 383184252,\n",
       " 384769112,\n",
       " 386358336,\n",
       " 387951930,\n",
       " 389549900,\n",
       " 391152252,\n",
       " 392758992,\n",
       " 394370126,\n",
       " 395985660,\n",
       " 397605600,\n",
       " 399229952,\n",
       " 400858722,\n",
       " 402491916,\n",
       " 404129540,\n",
       " 405771600,\n",
       " 407418102,\n",
       " 409069052,\n",
       " 410724456,\n",
       " 412384320,\n",
       " 414048650,\n",
       " 415717452,\n",
       " 417390732,\n",
       " 419068496,\n",
       " 420750750,\n",
       " 422437500,\n",
       " 424128752,\n",
       " 425824512,\n",
       " 427524786,\n",
       " 429229580,\n",
       " 430938900,\n",
       " 432652752,\n",
       " 434371142,\n",
       " 436094076,\n",
       " 437821560,\n",
       " 439553600,\n",
       " 441290202,\n",
       " 443031372,\n",
       " 444777116,\n",
       " 446527440,\n",
       " 448282350,\n",
       " 450041852,\n",
       " 451805952,\n",
       " 453574656,\n",
       " 455347970,\n",
       " 457125900,\n",
       " 458908452,\n",
       " 460695632,\n",
       " 462487446,\n",
       " 464283900,\n",
       " 466085000,\n",
       " 467890752,\n",
       " 469701162,\n",
       " 471516236,\n",
       " 473335980,\n",
       " 475160400,\n",
       " 476989502,\n",
       " 478823292,\n",
       " 480661776,\n",
       " 482504960,\n",
       " 484352850,\n",
       " 486205452,\n",
       " 488062772,\n",
       " 489924816,\n",
       " 491791590,\n",
       " 493663100,\n",
       " 495539352,\n",
       " 497420352,\n",
       " 499306106,\n",
       " 501196620,\n",
       " 503091900,\n",
       " 504991952,\n",
       " 506896782,\n",
       " 508806396,\n",
       " 510720800,\n",
       " 512640000,\n",
       " 514564002,\n",
       " 516492812,\n",
       " 518426436,\n",
       " 520364880,\n",
       " 522308150,\n",
       " 524256252,\n",
       " 526209192,\n",
       " 528166976,\n",
       " 530129610,\n",
       " 532097100,\n",
       " 534069452,\n",
       " 536046672,\n",
       " 538028766,\n",
       " 540015740,\n",
       " 542007600,\n",
       " 544004352,\n",
       " 546006002,\n",
       " 548012556,\n",
       " 550024020,\n",
       " 552040400,\n",
       " 554061702,\n",
       " 556087932,\n",
       " 558119096,\n",
       " 560155200,\n",
       " 562196250,\n",
       " 564242252,\n",
       " 566293212,\n",
       " 568349136,\n",
       " 570410030,\n",
       " 572475900,\n",
       " 574546752,\n",
       " 576622592,\n",
       " 578703426,\n",
       " 580789260,\n",
       " 582880100,\n",
       " 584975952,\n",
       " 587076822,\n",
       " 589182716,\n",
       " 591293640,\n",
       " 593409600,\n",
       " 595530602,\n",
       " 597656652,\n",
       " 599787756,\n",
       " 601923920,\n",
       " 604065150,\n",
       " 606211452,\n",
       " 608362832,\n",
       " 610519296,\n",
       " 612680850,\n",
       " 614847500,\n",
       " 617019252,\n",
       " 619196112,\n",
       " 621378086,\n",
       " 623565180,\n",
       " 625757400,\n",
       " 627954752,\n",
       " 630157242,\n",
       " 632364876,\n",
       " 634577660,\n",
       " 636795600,\n",
       " 639018702,\n",
       " 641246972,\n",
       " 643480416,\n",
       " 645719040,\n",
       " 647962850,\n",
       " 650211852,\n",
       " 652466052,\n",
       " 654725456,\n",
       " 656990070,\n",
       " 659259900,\n",
       " 661534952,\n",
       " 663815232,\n",
       " 666100746,\n",
       " 668391500,\n",
       " 670687500,\n",
       " 672988752,\n",
       " 675295262,\n",
       " 677607036,\n",
       " 679924080,\n",
       " 682246400,\n",
       " 684574002,\n",
       " 686906892,\n",
       " 689245076,\n",
       " 691588560,\n",
       " 693937350,\n",
       " 696291452,\n",
       " 698650872,\n",
       " 701015616,\n",
       " 703385690,\n",
       " 705761100,\n",
       " 708141852,\n",
       " 710527952,\n",
       " 712919406,\n",
       " 715316220,\n",
       " 717718400,\n",
       " 720125952,\n",
       " 722538882,\n",
       " 724957196,\n",
       " 727380900,\n",
       " 729810000,\n",
       " 732244502,\n",
       " 734684412,\n",
       " 737129736,\n",
       " 739580480,\n",
       " 742036650,\n",
       " 744498252,\n",
       " 746965292,\n",
       " 749437776,\n",
       " 751915710,\n",
       " 754399100,\n",
       " 756887952,\n",
       " 759382272,\n",
       " 761882066,\n",
       " 764387340,\n",
       " 766898100,\n",
       " 769414352,\n",
       " 771936102,\n",
       " 774463356,\n",
       " 776996120,\n",
       " 779534400,\n",
       " 782078202,\n",
       " 784627532,\n",
       " 787182396,\n",
       " 789742800,\n",
       " 792308750,\n",
       " 794880252,\n",
       " 797457312,\n",
       " 800039936,\n",
       " 802628130,\n",
       " 805221900,\n",
       " 807821252,\n",
       " 810426192,\n",
       " 813036726,\n",
       " 815652860,\n",
       " 818274600,\n",
       " 820901952,\n",
       " 823534922,\n",
       " 826173516,\n",
       " 828817740,\n",
       " 831467600,\n",
       " 834123102,\n",
       " 836784252,\n",
       " 839451056,\n",
       " 842123520,\n",
       " 844801650,\n",
       " 847485452,\n",
       " 850174932,\n",
       " 852870096,\n",
       " 855570950,\n",
       " 858277500,\n",
       " 860989752,\n",
       " 863707712,\n",
       " 866431386,\n",
       " 869160780,\n",
       " 871895900,\n",
       " 874636752,\n",
       " 877383342,\n",
       " 880135676,\n",
       " 882893760,\n",
       " 885657600,\n",
       " 888427202,\n",
       " 891202572,\n",
       " 893983716,\n",
       " 896770640,\n",
       " 899563350,\n",
       " 902361852,\n",
       " 905166152,\n",
       " 907976256,\n",
       " 910792170,\n",
       " 913613900,\n",
       " 916441452,\n",
       " 919274832,\n",
       " 922114046,\n",
       " 924959100,\n",
       " 927810000,\n",
       " 930666752,\n",
       " 933529362,\n",
       " 936397836,\n",
       " 939272180,\n",
       " 942152400,\n",
       " 945038502,\n",
       " 947930492,\n",
       " 950828376,\n",
       " 953732160,\n",
       " 956641850,\n",
       " 959557452,\n",
       " 962478972,\n",
       " 965406416,\n",
       " 968339790,\n",
       " 971279100,\n",
       " 974224352,\n",
       " 977175552,\n",
       " 980132706,\n",
       " 983095820,\n",
       " 986064900,\n",
       " 989039952,\n",
       " 992020982,\n",
       " 995007996,\n",
       " 998001000,\n",
       " ...]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time python_test(10000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 160 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([          0,           2,          12, ...,  1733449838,\n",
       "       -1862175748, -1102834048], dtype=int32)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time numpy_test(10000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "矩阵和随机数的生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  5,  8, 11, 14, 17])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(2,20,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2. ,  2.3,  2.6,  2.9,  3.2,  3.5,  3.8,  4.1,  4.4,  4.7,  5. ,\n",
       "        5.3,  5.6,  5.9,  6.2,  6.5,  6.8,  7.1,  7.4,  7.7,  8. ,  8.3,\n",
       "        8.6,  8.9,  9.2,  9.5,  9.8, 10.1, 10.4, 10.7, 11. , 11.3, 11.6,\n",
       "       11.9, 12.2, 12.5, 12.8, 13.1, 13.4, 13.7, 14. , 14.3, 14.6, 14.9,\n",
       "       15.2, 15.5, 15.8, 16.1, 16.4, 16.7, 17. , 17.3, 17.6, 17.9, 18.2,\n",
       "       18.5, 18.8, 19.1, 19.4, 19.7])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(2,20,0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#np.zeros\n",
    "np.zeros(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(10,dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#shape\n",
    "np.zeros(shape=(3,5),dtype = int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#np.ones\n",
    "np.ones(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(shape=(3,5),dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 99, 99, 99, 99, 99, 99, 99, 99, 99])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#np.full\n",
    "np.full(10,99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  2.22222222,  4.44444444,  6.66666667,  8.88888889,\n",
       "       11.11111111, 13.33333333, 15.55555556, 17.77777778, 20.        ])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#等差数列\n",
    "np.linspace(0,20,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#生成随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 5, 9, 3, 6])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10,size=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 0, 4, 9, 2],\n",
       "       [3, 5, 7, 9, 4]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10,size=(2,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 9, 9, 3])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(99)\n",
    "np.random.randint(1,10,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 9, 4, 8, 8])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(100)\n",
    "np.random.randint(1,10,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5263824674842963"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random()  # 0到1 的浮点数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.14860484, 0.15671108, 0.18646718, 0.21010774, 0.4527399 ],\n",
       "       [0.87014264, 0.06368104, 0.62431189, 0.52334774, 0.56229626],\n",
       "       [0.00581719, 0.30742321, 0.95018431, 0.12665424, 0.07898787]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random((3,5))  # 0到1 的浮点数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ndarray基础操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "A= np.ones(shape=(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 5)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#reshape 修改维度\n",
    "a = np.arange(10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=a.reshape(2,5)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(2,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(2,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5],\n",
       "       [6, 7],\n",
       "       [8, 9]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=a.reshape(-1,2)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##取值操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=np.arange(15).reshape(3,5)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[0][1] #不推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[(0,1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[0,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.arange(10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:8:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "二维数组的切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#前2行，前3列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [5, 6, 7]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[0:2,0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [5, 6, 7]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:2,:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 4],\n",
       "       [5, 7, 9]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:2,::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 13, 12, 11, 10],\n",
       "       [ 9,  8,  7,  6,  5],\n",
       "       [ 4,  3,  2,  1,  0]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[::-1,::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  5, 10],\n",
       "       [ 1,  6, 11],\n",
       "       [ 2,  7, 12],\n",
       "       [ 3,  8, 13],\n",
       "       [ 4,  9, 14]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#矩阵的合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1=np.array([[22,168],\n",
    "           [18,187]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "x2=np.array([[0],\n",
    "           [1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 22, 168,   0],\n",
       "       [ 18, 187,   1]])"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=np.concatenate([x1,x2],axis = 1)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "聚合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(15).reshape(3,5)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "105"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.0"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "L = np.array([[1.2,1.5,1.8],\n",
    "             [1.3,1.4,1.9],\n",
    "             [1.1,1.6,1.7]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "y=np.array([5,10,9]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6. , 15. , 16.2],\n",
       "       [ 6.5, 14. , 17.1],\n",
       "       [ 5.5, 16. , 15.3]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t=L[0:3]*y[0:3]\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([37.2, 37.6, 36.8])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l1=np.array([np.sum(t[0]),\n",
    "             np.sum(t[1]),\n",
    "             np.sum(t[2])])\n",
    "l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.56 µs ± 12.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "r1=np.dot(L,y)\n",
    "r1[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6. , 15. , 16.2])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6.5, 14. , 17.1])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5.5, 16. , 15.3])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.53 µs ± 12.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "r1=np.sum(t[0])\n",
    "r1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 8, 9, 5, 1, 2, 9, 3, 4, 2, 3, 8, 3, 7, 1, 3, 7, 7, 3, 8, 8, 1,\n",
       "       7, 6, 2, 5, 7, 1, 7, 6])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C=np.random.randint(1,10,size=30)\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 8, 9],\n",
       "       [5, 1, 2],\n",
       "       [9, 3, 4],\n",
       "       [2, 3, 8],\n",
       "       [3, 7, 1],\n",
       "       [3, 7, 7],\n",
       "       [3, 8, 8],\n",
       "       [1, 7, 6],\n",
       "       [2, 5, 7],\n",
       "       [1, 7, 6]])"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D=C.reshape(10,3)\n",
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 5, 9, 2, 3, 3, 3, 1, 2, 1]])"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D[:10,:1]\n",
    "e=D[:10,:1].T\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转化前：\n",
      "[[2 5 9 2 3 3 3 1 2 1]]\n",
      "转化后：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 1, 0, 0, 0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_arr = e\n",
    "print(\"转化前：\")\n",
    "print(np_arr)\n",
    "print(\"转化后：\")\n",
    "t = np.int64(np_arr>3)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [1],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0]])"
      ]
     },
     "execution_count": 242,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = np.int32(np_arr>3)\n",
    "z=t.T\n",
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 8],\n",
       "       [5, 1],\n",
       "       [9, 3],\n",
       "       [2, 3],\n",
       "       [3, 7],\n",
       "       [3, 7],\n",
       "       [3, 8],\n",
       "       [1, 7],\n",
       "       [2, 5],\n",
       "       [1, 7]])"
      ]
     },
     "execution_count": 238,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p=D[:10,0:2]\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 8],\n",
       "       [1, 5, 1],\n",
       "       [1, 9, 3],\n",
       "       [0, 2, 3],\n",
       "       [0, 3, 7],\n",
       "       [0, 3, 7],\n",
       "       [0, 3, 8],\n",
       "       [0, 1, 7],\n",
       "       [0, 2, 5],\n",
       "       [0, 1, 7]])"
      ]
     },
     "execution_count": 244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.concatenate([z,p],axis = 1)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
